/*
 * @Author: linzaifei 1609781601@qq.com
 * @Date: 2025-07-24 13:28:26
 * @LastEditors: linzaifei 1609781601@qq.com
 * @LastEditTime: 2025-08-15 13:46:52
 * @FilePath: /amap-web-vr/src/views/home/control/vc-pitch-control/useCesiumPitch.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

import * as Cesium from 'cesium';
// import {useCesiumCenter} from './useCesiumCenter'
// const {getCenter} = useCesiumCenter()
export const useCesiumPitch = () => {
    
    const pitchControl = (viewer: Cesium.Viewer) => {
            if (!viewer) throw new Error('地图未初始化');
            
            // 获取当前相机
            const camera = viewer.camera;
            let isRightMouseDown = false;
            
            // 监听鼠标右键按下事件
            viewer.screenSpaceEventHandler.setInputAction(() => {
                isRightMouseDown = true;
            }, Cesium.ScreenSpaceEventType.RIGHT_DOWN);
            
            // 监听鼠标右键释放事件
            viewer.screenSpaceEventHandler.setInputAction(() => {
                isRightMouseDown = false;
            }, Cesium.ScreenSpaceEventType.RIGHT_UP);
            
            // 监听鼠标移动事件
            viewer.screenSpaceEventHandler.setInputAction((movement: any) => {
                if (!isRightMouseDown) return;
                
                // 获取鼠标移动的像素距离
                const deltaY = movement.endPosition.y - movement.startPosition.y;
                
                // 计算新的pitch角度（弧度）
                const currentPitch = camera.pitch;
                // 根据鼠标移动方向调整pitch
                // 向上拖动增加pitch（显示更多地形），向下拖动减少pitch
                const newPitch = currentPitch - deltaY * 0.01;
                
                // 限制pitch的范围，防止过度倾斜
                const clampedPitch = Math.max(-Math.PI/2, Math.min(Math.PI/2, newPitch));
                
                
                let cartographic =  Cesium.Ellipsoid.WGS84.cartesianToCartographic(viewer.camera.position);

                const longitude = Cesium.Math.toDegrees(cartographic.longitude);
                const latitude = Cesium.Math.toDegrees(cartographic.latitude);
                // console.log("当前俯仰角:", longitude,latitude);

                // 设置新的相机角度
                camera.setView({
                    destination : Cesium.Cartesian3.fromDegrees(longitude,latitude, cartographic.height),
                    orientation: {
                        heading: camera.heading,
                        pitch: clampedPitch,
                        roll: camera.roll
                    }
                });
            }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
    }

    return {
        pitchControl
    }
}